package com.xiaotu.spider.project.pageprocessor.list;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.util.*;
import com.xiaotu.spider.AUrlPageProcessor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * 网大名称，年份，产地，类型，点播量，评分，影片简介，视频时间
 */
@Component("QQListPageProcessor")
public class QQListPageProcessor extends AUrlPageProcessor
{
	private static String[] YEARS = new String[3];
	
	static {
		List<String> yearsList = new ArrayList<String>();
		yearsList.add("100063");
		yearsList.addAll(Arrays.asList(DateUtils.getRecentYears(2)));
		YEARS = yearsList.toArray(YEARS);
	}
	
    private static final String[] LIST_URLS = new String[]{
            "http://v.qq.com/x/list/movie?year=%s&cate=10001&offset=0&area=-1&subtype=-1",
            "http://v.qq.com/x/list/movie?cate=10001&area=-1&subtype=-1&year=%s&offset=0&sort=19&theatre=1"};

    @Override
    public void process(Page page)
    {
        if (page.getUrl().toString().contains("http://v.qq.com/x/list/movie"))
            this.listProcess(page);
        else
            pageProcess(page);
    }

    private void listProcess(Page page)
    {
        Html html = page.getHtml();
        List<String> divList = html.xpath("//ul[@class='figures_list']/li").all();
        if (divList == null || divList.isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);

        page.getRequest().putExtra("type", page.getUrl().toString().contains("theatre=1") ? "院线" : "非院线");
        divList.forEach(divStr -> PageUtils.addTargetRequest(page, new Html(divStr).xpath
                ("//a[@class='figure']/@href").get(), page.getRequest().getExtras()));

        String url = html.xpath("//div[@class='mod_pages']//a[@class='page_next']/@href").get();
        if (StringUtils.isNotEmpty(url) && !url.contains("javascript"))
            PageUtils.addTargetRequest(page, url, page.getRequest().getExtras());

    }

    private void pageProcess(Page page)
    {
        Html html = page.getHtml();
        List<String> tagList = html.xpath("//div[@class='video_tags']//a[@_stat='intro:tag']/text()").all();
        if (tagList == null || tagList.size() < 2)
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        String title = html.xpath("//a[@_stat='intro:title']/@title").get();
        String score = html.xpath("//span[@class='video_score']/span[@class='units']/text()").get() + html.xpath
                ("//span[@class='video_score']/span[@class='decimal']/text()").get();
        String play = html.xpath("//em[@id='mod_cover_playnum']/text()").get();
        String desc = html.xpath("//p[@class='summary']/text()").get();
        String vodeoTime = html.xpath("//div[@class='figure_count']/span/text()").get();

        String line = StringUtils.join(new String[]{StringUtil.nullToString(page.getRequest().getExtra("type")),
                title, tagList.get(1), tagList.get(0), tagList.size() > 2 ? StringUtils.join(tagList.subList(2,
                tagList.size()), SepratorUtil.SEP_COMMA_EN) : StringUtils.EMPTY, play, score, desc, StringUtil
                .nullToString(vodeoTime)}, SepratorUtil.SEP_CSV_FIELDS);
        System.out.println(line);
        FileUtil.writeFile("D://qq.csv", line + SepratorUtil.LINE_SEPRATOR);
    }

    @Override
    protected String[] getStartRequestUrls()
    {
        List<String> list = new ArrayList<>();
        Stream.of(YEARS).forEach(year -> Stream.of(LIST_URLS).forEach(url -> list.add(String.format(url, year))));
        return list.toArray(new String[list.size()]);
    }

    @Override
    protected String getSpiderType()
    {
        return "QQ_LIST";
    }

    @Override
    public String getPageDataType(Page page)
    {
        return Constants.PageDataType.SEARCH_LIST;
    }
}
